<?php

namespace Modules\System\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Modules\Chinadmin\Services\C;
use Modules\System\Services\Mds\MDSArray;
use Illuminate\Routing\Controller;

class Common extends Controller
{

    public function __construct(){

    }

    /**
     * 万能查询接口。
     * http://chejiuxing.chinadmin.top/system/admin/common/query
     *
     * "Modules":"Chinadmin",
     * "model":"Employee",
     * "where_arr":"{}",                     多条件
     * "type":"one/list",                    一条还是多条
     *
     */
    public function query(Request $request){

        $i = $request->input();

        if(!isset($i['type'])){
            $i['type'] = "list";
        }

        //这里要限制可修改的模型和数据，暂且不限制，后期添加限制。
        if(empty($i['model'])){
            mdsResponse(400,"必须传model");
        }

        //确定执行的模型
        $model = C::$m[$i['model']]??"";

        if(empty($model)){
            $i['Modules'] = $i['Modules']??"Chinadmin";
            $model = "Modules\\".$i['Modules']."\Models\\".$i['model']."Model";
        }

        $list = $model::where(function($query) use ($i){
            foreach($i['where_arr'] as $k=>$vv){
                $query->where($vv['field'],$vv['condition'],$vv['val']);
            }
        })->get();

        if($i['type'] == 'one'){
            return $list[0];
        }else{
            return $list;
        }

    }

    /**
     * 万能更新值接口。
     * http://l.com/chinadmin/admin/common/update
     *
     * "model":"Employee",
     * "id_field":"employee_id",
     * "id_val": 1,   单条件
     * "up_field":"status",             单更新
     * "up_val":2,
     *
     * "model":"Employee",
     * "id_field":"employee_id",        单条件
     * "id_val": 1,                     单条件
     * "up_arr":"{}",                   单更新
     *
     * "model":"Employee",
     * "where_arr":"{}",                多条件
     * "up_arr":"{}",                   多更新
     *
     */
    public function update(Request $request){

        $i = $request->input();

        //这里要限制可修改的模型和数据，暂且不限制，后期添加限制。
        if(empty($i['model'])){
            mdsResponse(400,"必须传model");
        }

        //确定执行的模型
        $model = C::$m[$i['model']]??"";

        if(empty($model)){
            $i['Modules'] = $i['Modules']??"Chinadmin";
            $model = "Modules\\".$i['Modules']."\Models\\".$i['model']."Model";
        }

        //1
        if(isset($i['up_field'])){

            if(empty($i['id_field'])){
                mdsResponse(400,"必须传up_field");
            }
            if(empty($i['id_val'])){
                mdsResponse(400,"必须传up_val");
            }
            if(empty($i['up_field'])){
                mdsResponse(400,"必须传up_field");
            }
            if(empty($i['up_val'])){
                mdsResponse(400,"必须传up_val");
            }

            $i['where_arr'] = [
                $i['id_field'] => $i['id_val']
            ];

            $i['up_arr'] = [
                $i['up_field'] => $i['up_val']
            ];

        }else{
            //2
            if(!isset($i['where_arr'])){

                if(empty($i['id_field'])){
                    mdsResponse(400,"必须传id_field");
                }
                if(empty($i['id_val'])){
                    mdsResponse(400,"必须传id_val");
                }

                $i['where_arr'] = [
                    $i['id_field'] => $i['id_val']
                ];

            }
        }

        //修改多条数据
        if(empty($i['where_arr'])){
            mdsResponse(400,"必须传更新条件");
        }

        //修改多条数据
        if(empty($i['up_arr'])){
            mdsResponse(400,"必须传更新数据");
        }

        foreach($i['up_arr'] as $k => $v){
            if($v == 'ca_date'){                                //ca_date是时间
                $i['up_arr'][$k] = date("Y-m-d H:i:s");
            }
        }

        $re = $model::where(function($query) use ($i){
            foreach($i['where_arr'] as $k=>$id_val){
                //如果条件值是一个数组，那就是第一种情况中取数组中的一列in为筛选。
                if(isset($id_val) && gettype($id_val) == "array"){
                    $id_val = array_column($id_val, $k);
                    $query->whereIn($k,$id_val);
                }else{
                    $query->where($k,$id_val);
                }
            }
        })->update($i['up_arr']);

        mds_if_response($re);

    }

}
